/*
 * Copyright (c) 2007, 2008 fr3@K <freak@fsfoundry.org>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef GION__AUTO_HANDLE_HPP
#define GION__AUTO_HANDLE_HPP

#include <algorithm> // std::swap

namespace gion
{

  namespace bits
  {
    template <class HandleTraits>
    struct auto_handle_rc
    {
      typedef HandleTraits traits_type;
      typedef typename HandleTraits::handle_type handle_type;

      explicit auto_handle_rc(
        handle_type handle,
        bool empty)
        : handle(handle),
          empty(empty)
      {}
    
      handle_type handle;
      bool empty;
    };
  } // namespace bits

  template <class HandleTraits>
  class auto_handle;

  template <class HandleTraits>
  class auto_handle_cref
  {
  public:
    // public interface
    typedef HandleTraits traits_type;
    typedef typename HandleTraits::handle_type handle_type;

    handle_type get() const
    {
      return handle_;
    }
    bool empty() const
    {
      return empty_;
    }

  private:
    template <class>
    friend class auto_handle;

    auto_handle_cref(
      handle_type h,
      bool empty)
      : handle_(h),
        empty_(empty)
    {}

    // disables assignment
    auto_handle_cref& operator=(const auto_handle_cref&);

    handle_type handle_;
    bool empty_;
  };

  template <class HandleTraits>
  class auto_handle
  {
  public:
    // public interface
    typedef HandleTraits traits_type;
    typedef typename HandleTraits::handle_type handle_type;
    typedef auto_handle_cref<HandleTraits> const_reference;

    // std::auto_ptr like interface
    auto_handle()
      : handle_(),
        empty_(true)
    {
    }
    explicit auto_handle(handle_type handle)
      : handle_(handle),
        empty_(false)
    {
    }
    auto_handle(auto_handle& other)
      : handle_(other.get()),
        empty_(other.empty())
    {
      other.release();
    }
    auto_handle& operator=(auto_handle& other)
    {
      if(other.empty())
        this->reset();
      else
        this->reset(other.release());

      return *this;
    }
    ~auto_handle()
    {
      this->reset();
    }

    handle_type get() const
    {
      return handle_;
    }

    handle_type release()
    {
      empty_ = true;
      return handle_;
    }

    void reset(handle_type handle)
    {
      if(empty_ == false)
      {
        traits_type::deallocate(this->release());
      }
      empty_ = false;
      handle_ = handle;
    }

    void reset()
    {
      if(empty_ == false)
        traits_type::deallocate(this->release());
    }

    bool empty() const
    {
      return empty_;
    }

    const_reference get_ref() const
    {
      return const_reference(handle_, empty_);
    }

    void swap(auto_handle& other)
    {
      using std::swap;
      swap(handle_, other.handle_);
      swap(empty_, other.empty_);
    }

    // MAGIC
    auto_handle(bits::auto_handle_rc<traits_type> other_rc)
      : handle_(other_rc.handle),
        empty_(other_rc.empty)
    {
    }
    auto_handle& operator=(bits::auto_handle_rc<traits_type> other_rc)
    {
      if(other_rc.empty)
        this->reset();
      else
        this->reset(other_rc.handle);

      return *this;
    }
    operator bits::auto_handle_rc<traits_type>()
    {
      bits::auto_handle_rc<traits_type> ref(handle_, empty_);
      this->release();
      return ref;
    }

  private:
    handle_type handle_;
    bool empty_;
  };

  template <class HandleTraits>
  void swap(
    auto_handle<HandleTraits>& lhs,
    auto_handle<HandleTraits>& rhs)
  {
    lhs.swap(rhs);
  }

} // namespace gion

#endif // !GION__AUTO_HANDLE_HPP
